 /**
 * Description: 实施资料管理
 * @author:jacky
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-05-02   jacky       1.0.0     Initial
 */
package com.metro.demo.material.service.impl;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.demo.common.beans.constant.PropertiesEnum;
import com.metro.demo.material.beans.vo.BizMaterialManage;
import com.metro.demo.material.mapper.IBizMaterialManageMapper;
import com.metro.demo.material.service.inf.IBizMaterialManageService;
 /**
 * 实施资料管理<br>
 *
 * @author:jacky
 * @since:1.0.0
 * @version [1.0.0, 2018-05-02]
 */
@Service("bizmaterialmanageService")
public class BizMaterialManageService extends BaseServiceSupport<BizMaterialManage> implements IBizMaterialManageService{
    //实施资料管理Mapper
    @Autowired
    private IBizMaterialManageMapper bizmaterialmanageMapper;

    @Override
    public IMapper getMapper() {
        return bizmaterialmanageMapper;
    }

    @Override
    public IResult save(BizMaterialManage entity) {
        try {
            bizmaterialmanageMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增实施资料管理失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(BizMaterialManage entity) {
        try {
            bizmaterialmanageMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改实施资料管理失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            bizmaterialmanageMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            bizmaterialmanageMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    //查询图片路径
	@Override
	public List<BizMaterialManage> getBycode(String code) {
		
		 try {
			 List<BizMaterialManage> list = bizmaterialmanageMapper.queryByCode(code);
	            return list;
	        } catch (Exception e) {
	            log.error("查询失败", e);
	            throw new ServiceException(ResultCode.ERROR);
	        }
	}
	//删除图片文件
	 @Override
	    public IResult deleteByCode(BizMaterialManage entity) {
	        try {
	        	//删除数据库数据
	            bizmaterialmanageMapper.deleteByCode(entity);
	          //删除本地图片 删除到code目录
	            String deletePath;
	            if(entity.getType().equals("1")) {
		            deletePath = PropertiesEnum.getPropertyValue(PropertiesEnum.FILE_UPLOAD_ROOT)+"/implMaterial/image/"+entity.getCode();  
	            }else {
	            	deletePath = PropertiesEnum.getPropertyValue(PropertiesEnum.FILE_UPLOAD_ROOT)+"/implMaterial/file/"+entity.getCode();  
	            }
	            //删除文件夹
	            delFolder(deletePath);
	            return ResultCode.SUCCESS;
	        } catch (Exception e) {
	            log.error("删除失败", e);
	            throw new ServiceException(ResultCode.ERROR);
	        }
	    }
	 
	//删除文件夹  
	 public static void delFolder(String folderPath) {  
	      try {  
	         delAllFile(folderPath); //删除完里面所有内容  
	         String filePath = folderPath;  
	         filePath = filePath.toString();  
	         java.io.File myFilePath = new java.io.File(filePath);  
	         myFilePath.delete(); //删除空文件夹  
	      } catch (Exception e) {  
	        e.printStackTrace();   
	      }  
	 } 
	 
	 public static boolean delAllFile(String path) {  
	       boolean flag = false;  
	       File file = new File(path);  
	       if (!file.exists()) {  
	         return flag;  
	       }  
	       if (!file.isDirectory()) {  
	         return flag;  
	       }  
	       String[] tempList = file.list();  
	       File temp = null;  
	       for (int i = 0; i < tempList.length; i++) {  
	          if (path.endsWith(File.separator)) {  
	             temp = new File(path + tempList[i]);  
	          } else {  
	              temp = new File(path + File.separator + tempList[i]);  
	          }  
	          if (temp.isFile()) {  
	             temp.delete();  
	          }  
	          if (temp.isDirectory()) {  
	             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件  
	             delFolder(path + "/" + tempList[i]);//再删除空文件夹  
	             flag = true;  
	          }  
	       }  
	       return flag;  
	     }

	 //判断记录是否已存在
	 public IResult checkCode(String code,String type) {
	    	//判断是否已存在
	    	List<BizMaterialManage> list = bizmaterialmanageMapper.queryCodeList(type);
	    	if(list != null && list.size()>0 ) {
	    		for(int i=0;i<list.size();i++){
	        		if(list.get(i).getCode().equals(code)) {
	        			return ResultCode.DUPLICATE_KEY;
	        		}
	        	}
	    	}
	    	return ResultCode.SUCCESS;
	    }

	//查询图片路径
		@Override
		public String getPath(BizMaterialManage entity) {
			
			 try {
				 String path = bizmaterialmanageMapper.queryPath(entity);
		            return path;
		        } catch (Exception e) {
		            log.error("查询失败", e);
		            throw new ServiceException(ResultCode.ERROR);
		        }
		}
		
		
		//批量下载查询图片路径
				@Override
				public List<String> getPaths(BizMaterialManage entity) {
					
					 try {
						 List<String> paths = bizmaterialmanageMapper.queryPaths(entity);
				            return paths;
				        } catch (Exception e) {
				            log.error("查询失败", e);
				            throw new ServiceException(ResultCode.ERROR);
				        }
				}
}